package com.yixun.qfbao.controller.agency.weapp;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import com.yixun.qfbao.api.service.CPService;
import com.yixun.qfbao.api.service.CommissionService;
import com.yixun.qfbao.apidto.GetCustomerPremisesReqDto;
import com.yixun.qfbao.apidto.GetCustomerPremisesResDto;
import com.yixun.qfbao.apidto.GetPerCPCommissionListReqDto;
import com.yixun.qfbao.apidto.GetPerCPCommissionListResDto;
import com.yixun.qfbao.apidto.base.CommonReqDTO;
import com.yixun.qfbao.request.PageRequest;
import com.yixun.qfbao.request.commission.CommissionListReqDto;
import com.yixun.qfbao.response.BaseResponse;
import com.yixun.qfbao.response.commission.CommissionInfoResDto;
import com.yixun.qfbao.response.commission.CommissionListResDto;
import com.yixun.qfbao.xf.model.dto.CustomerPremisesDto;
import com.yixun.qfbao.xf.model.enums.ResultEnum;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author zilong.liu
 * @date 2020-9-11
 */

@Controller
@ResponseBody
@RequestMapping("/agency/weapp/commision")
@Api(tags = {"分销小程序-佣金"})
public class CommissionController {

	@Autowired
	CommissionService commissionService;
	
	@Autowired
    CPService cpService;
	
	@ApiOperation(notes = "获取佣金金额", value="获取佣金金额")
    @PostMapping("/getCommisionInfo")
    public BaseResponse<CommissionInfoResDto> getCommisionInfo(@RequestBody @Valid CommonReqDTO request) {
        BaseResponse<CommissionInfoResDto> response = new BaseResponse<>();
        GetCustomerPremisesReqDto getCustomerPremisesReqDto = new GetCustomerPremisesReqDto();
        getCustomerPremisesReqDto.setIsturn(false);
        getCustomerPremisesReqDto.setUserId(request.getUserId());
        GetCustomerPremisesResDto getCustomerPremisesResDto = commissionService.getCustomerPremises(getCustomerPremisesReqDto);
        
        CommissionInfoResDto commissionInfoResDto = new CommissionInfoResDto(getCustomerPremisesResDto);
        response.setData(commissionInfoResDto);
        response.setCode(ResultEnum.SUCCESS.getNo());
        response.setMsg(ResultEnum.SUCCESS.getDes());
        return response;
    }
	
	@ApiOperation(notes = "获取佣金列表", value="获取佣金列表")
    @PostMapping("/getCommisionList")
    public BaseResponse<CommissionListResDto> getCommisionList(@RequestBody @Valid PageRequest<CommissionListReqDto> request) {
        BaseResponse<CommissionListResDto> response = new BaseResponse<>();
        
        GetPerCPCommissionListReqDto getPerCPCommissionListReqDto = new GetPerCPCommissionListReqDto();
        getPerCPCommissionListReqDto.setPageNo(request.getPageDTO().getPageNum());
        getPerCPCommissionListReqDto.setPageSize(request.getPageDTO().getPageSize());
        getPerCPCommissionListReqDto.setUserId(request.getUserId());
        getPerCPCommissionListReqDto.setLoginType(request.getLoginType());
        String commissionStatus = request.getSearchDTO().getCommissionStatus();
        getPerCPCommissionListReqDto.setCommissionStatus(commissionStatus);
        
        if(!"0".equals(commissionStatus) && !"1".equals(commissionStatus) 
        		&& !"2".equals(commissionStatus) && !"3".equals(commissionStatus)) {
        	response.setCode(ResultEnum.FAIL.getNo());
            response.setMsg(ResultEnum.FAIL.getDes());
            return response;
        }
        
        GetPerCPCommissionListResDto getPerCPCommissionListResDto = cpService.getPerCPCommissionList(getPerCPCommissionListReqDto);
        
        List<CustomerPremisesDto> customerPremisesDtos = getPerCPCommissionListResDto.getCustomerPremisesDtoPage().getRows();
        CommissionListResDto commissionListResDto = new CommissionListResDto();
        if(!CollectionUtils.isEmpty(customerPremisesDtos)) {
        	commissionListResDto.addCommissionListResDto(customerPremisesDtos);
        }
        response.setData(commissionListResDto);
        
        response.setCode(ResultEnum.SUCCESS.getNo());
        response.setMsg(ResultEnum.SUCCESS.getDes());
        return response;
    }
}
